<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2019-2020 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace app\admin\model;

use think\Model;
use app\admin\model\Themes;

class Config extends Model
{
    // 设置json类型字段
    protected $json = ['value'];

    // 设置JSON数据返回数组
    protected $jsonAssoc = true;
    
    // 搜索器
    public function searchLanguageAttr($query, $value, $array)
    {
        $language = request()->lang;
        $query->where("language", $language);
    }
    
    public function searchNameAttr($query, $value, $array)
    {
        if (! empty($value)) {
            $query->where("name", $value);
        }
    }

    /**
     * 配置设置
     */
    public static function setVal($name, $title, $value)
    {
        $where[] = ['name', '=', $name];
        $save = self::where($where)->find();
        if ($save) {
            $save->value = $value;
            $save->save();
        } else {
            self::create([
                'name'     => $name,
                'title'    => $title,
                'value'    => $value,
            ]);
        }
        cache($name, NULL);
    }

    /**
     * 配置获取
     */
    public static function getVal($name)
    {
        switch ($name) {
            case 'themes':
                $template  = [config('app.pc_template'),config('app.mp_template')];
                $themes    = Themes::whereIn('name', $template)->field('name,title,config')->select();
                $themesLen = count($themes);
                foreach ($themes as $index => $theme) {
                    $name   = 'theme_' . $theme['name'] . '_' . request()->lang;
                    $config = self::getVal($name);
                    $value  = $theme->config;
                    foreach ($theme['config'] as $key => $val) {
                        $valueConfig = isset($config[$val['field']]) ? $config[$val['field']] : $val['value'];
                        $value[$key]['value'] = $val['name'] === 'editor' ? content_replace($valueConfig) : $valueConfig;
                    }
                    if ($themesLen === 1) {
                        $theme->tips = '主题配置'; 
                    } else {
                        $theme->tips = config('app.pc_template') === $theme['name'] ? '主题配置-电脑' : '主题配置-手机'; 
                    }
                    $theme->config = $value;
                }
                return $themes;
                break;
            default:
                $where[] = ['name', '=', $name];
                $config  = self::where($where)->cache($name)->find();
                return $config ? $config->value : [];
                break;
        }
    }
}